#!/usr/bin/env node

debug  = require('debug')('luckpay-web:server')
http   = require 'http'
https  = require 'https'
webapp = require './webapp'

on_server_error = (error) ->
    throw error if error.syscall isnt 'listen'
    switch error.code
        when 'EACCES'
            console.error '绑定端口 ' + settings.listen_port + ' 需要特殊权限.'
            process.exit 1
        when 'EADDRINUSE'
            console.error '端口 ' + settings.listen_port + ' 已被占用.'
            process.exit 1
        else
            throw error

on_server_listening = ->
    debug '监听端口 ' + server.address().port

server = http.createServer webapp

server.listen settings.listen_port, settings.listen_addr
server.on 'error', on_server_error
server.on 'listening', on_server_listening
